#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->comboBoxtargetip->addItem(getHostIp());
    udpsocket=new QUdpSocket(this);
    connect(udpsocket,&QUdpSocket::readyRead,this,&MainWindow::readData);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString MainWindow::getHostIp()
{
    QString hostname=QHostInfo::localHostName();
    QHostInfo host=QHostInfo::fromName(hostname);
    QString hostip="";
    QList<QHostAddress> ls=host.addresses();
    if(!ls.isEmpty())
    {
        for(int i=0;i<ls.count();i++)
        {
            QHostAddress temp=ls.at(i);
            if(temp.protocol()==QAbstractSocket::IPv4Protocol)
            {
                hostip=temp.toString();
                break;
            }
        }

    }
   return hostip;
}


void MainWindow::on_pushButton_start_clicked()
{
    QString ip=ui->comboBoxtargetip->currentText();
    quint16 port=ui->spinBoxbindport->value();
    if(udpsocket->bind((QHostAddress)ip,port))
     {   ui->plainTextEditdispmsg->appendPlainText("**********绑定成功**********");
        ui->plainTextEditdispmsg->appendPlainText("$$$$$$$$$$绑定端口$$$$$$$$$$："+
                                                  QString::number(udpsocket->localPort()));
        ui->pushButton_start->setEnabled(false);
        ui->pushButton_stop->setEnabled(true);

    }
    else
    {
        ui->plainTextEditdispmsg->appendPlainText("**********绑定失败**********");
    }

}

void MainWindow::on_pushButton_stop_clicked()
{
     udpsocket->abort();
     ui->pushButton_start->setEnabled(true);
     ui->pushButton_stop->setEnabled(false);
       ui->plainTextEditdispmsg->appendPlainText("**********绑定断开**********");
}

void MainWindow::on_pushButton_sendmsg_clicked()
{
    QString msg=ui->lineEditmsg->text();
    QByteArray str=msg.toUtf8();
    QHostAddress targetaddress(ui->comboBoxtargetip->currentText());
    quint16 targetport=ui->spinBoxtargetport->value();
    udpsocket->writeDatagram(str,targetaddress,targetport);
    ui->plainTextEditdispmsg->appendPlainText("[out]："+str);
    ui->lineEditmsg->clear(); // 清除编辑框控件内容
    ui->lineEditmsg->setFocus(); // 将光标焦点定位到编辑框控件


}

void MainWindow::on_pushButton_broadcastmsg_clicked()
{
    QString msg=ui->lineEditmsg->text();
    QByteArray str=msg.toUtf8();
    quint16 targetport=ui->spinBoxtargetport->value();
    udpsocket->writeDatagram(str,QHostAddress::Broadcast,targetport);
    ui->plainTextEditdispmsg->appendPlainText("[broadcast]："+str);
    ui->lineEditmsg->clear(); // 清除编辑框控件内容
    ui->lineEditmsg->setFocus(); // 将光标焦点定位到编辑框控件
}

void MainWindow::readData()
{
    quint16 sendport;
    QHostAddress sendip;
    QByteArray str;
    udpsocket->readDatagram(str.data(),str.size(),&sendip,&sendport);
    QString strs=str.data();
    QString peer="[From]:"+sendip.toString()+QString::number(sendport)+":  ";
    ui->plainTextEditdispmsg->appendPlainText(peer+strs);
}
